package com.yunti.springboot.controller;

import com.yunti.springboot.annotation.Log;
import com.yunti.springboot.annotation.Permission;
import com.yunti.springboot.annotation.RepeatSubmit;
import com.yunti.springboot.po.Student;
import com.yunti.springboot.service.StudentService;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.SearchStudentReq;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    StudentService studentService;

    /**
     * 批量获取学生基本信息
     * @param searchStudentReq
     * @return
     */
    @PostMapping("/getStudentsInfo")
    public Result getStudentsInfo(@RequestBody SearchStudentReq searchStudentReq){
        return studentService.getStudentsInfo(searchStudentReq);
    }

    /**
     * 通过id获取学生考试信息
     * @param searchStudentReq
     * @return
     */
    @PostMapping("/getStudentExamInfoById")
    public Result getStudentExamInfoById(@RequestBody SearchStudentReq searchStudentReq){
        return studentService.getStudentExamInfoById(searchStudentReq);
    }


    /**
     * 批量获取学生考试信息
     * @param searchStudentReq
     * @return
     */
    @PostMapping("/getStudentsExamInfo")
    public Result getStudentsExamInfo(@RequestBody SearchStudentReq searchStudentReq){
        return studentService.getStudentsExamInfo(searchStudentReq);
    }

    /**
     * 保存或修改学生基本信息
     * @param student
     * @return
     */
    @Log
    @RepeatSubmit(expireSeconds = 3)
    @PostMapping("/saveOrUpdateStu")
    public Result saveOrUpdateStuInfo(@RequestBody Student student){
        return studentService.saveOrupdateStuInfo(student);
    }

    @Log
    @PostMapping("/importStuList")
    @Transactional(rollbackFor = RuntimeException.class)
    @Permission({"studentManage"})
    @RepeatSubmit(expireSeconds = 10)
    public Result importStuList(@RequestParam("file")MultipartFile file){
        return studentService.importStuList(file);
    }

    @Log
    @PostMapping("/adminImportStuList")
    @Transactional(rollbackFor = RuntimeException.class)
    @Permission({"superAdmin"})
    @RepeatSubmit(expireSeconds = 10)
    public Result adminImportStuList(@RequestParam("file")MultipartFile file,
                                     @RequestParam("schoolId")Integer schoolId){
        return studentService.adminImportStuList(file,schoolId);
    }


    /**
     * 导出学生信息(成绩卡)
     * @param req
     * @return
     */
    @PostMapping("/printStuExamInfoById")
    public void printStuExamInfoById(@RequestBody SearchStudentReq req, HttpServletResponse response){
        studentService.printStuExamInfoById(req,response);
    }

    /**
     * 获取考生考试信息，包含成绩等信息
     * @param req
     * @return
     */
    @PostMapping("/getStuExamInfoById")
    public Result getStuExamInfoById(@RequestBody SearchStudentReq req){
        return studentService.getStuExamInfoById(req);
    }


    /**
     * 通过id和学期获取所有科目（选修和必修）
     * @param searchStudentReq
     * @return
     */
    @PostMapping("/getAllSubById")
    public Result getAllSubById(@RequestBody SearchStudentReq searchStudentReq){
        return studentService.getAllSubById(searchStudentReq);
    }
}
